
var kolom ={};
var kolomkaku={};
var url1;
var ID;
var pagesizeboy;
//var grid = "#"+$grid+"_Grid";
var tBar={};
var val_na;
var tabel="";
var w_window;
var h_window;
		var create_from;
		var create_to;
		var today = new Date();
		var dd = today.getDate();
		var mm = today.getMonth()+1; //January is 0!
		
		var yyyy = today.getFullYear();
		
		//$('#tool_vr').css('width',frmWidth-20)
		if(dd<10){dd='0'+dd} 
		if(mm<10){mm='0'+mm}
		today = yyyy+'-'+mm+'-'+dd;
		val_date = today;
		//console.log(today);


switch(modul_anak){
	case "pemesanan":
		$('#tgl_awal_'+modul_anak).datebox({formatter: formatDate });
		$('#tgl_akhir_'+modul_anak).datebox({formatter: formatDate });
		$('#tgl_awal_'+modul_anak).datebox('setValue',today);
		$('#tgl_akhir_'+modul_anak).datebox('setValue',today);
		
       // url1 = 
		w_window=frmWidth-280;
		h_window=frmHeight-252;
		kolom[modul_anak] = [
			{field:'ck', checkbox:true},
            {field:'no_permintaan',title:'No. Permintaan',hidden:false,width:150},
			{field:'tgl_permintaan',title:'Tgl. Permintaan',width:100,align:'center'},
			{field:'no_pemesanan',title:'No. Pemesanan',hidden:false,width:150},
			{field:'tgl_pemesanan',title:'Tgl. Pemesanan',width:100,align:'center'},
			{field:'nama_supplier',title:'Supplier',width:200,align:'left'},
			{field:'pemesanan_via',title:'Pemesanan VIA',width:250},							
			{field:'sistem_pengiriman',title:'Sistem Pengiriman',width:250},	
			{field:'flag',title:'Status',width:150,align:'center',
				styler:function(index,rowData){  
					if(rowData.flag == 'P'){
						return 'background-color:#99B700;color:#F3F3F3';
					}else if(rowData.flag == 'B'){
						return 'background-color:red;color:#F3F3F3';
					}else if(rowData.flag == 'C'){
						return 'background-color:#F7E15A;color:#F3F3F3';
					}else if(rowData.flag == 'R'){
						return 'background-color:#A67334;color:#F3F3F3';
					}else if(rowData.flag == 'D'){
						return 'background-color:red;color:#F3F3F3';
					}else{
						return '';
					}
							
				},
				formatter:function(value,rowData,rowIndex){
					if(rowData.flag == 'P'){
						return 'Proses Pembuatan Faktur';
					}else if(rowData.flag == 'B'){
						return 'Batal';
					}else if(rowData.flag == 'C'){
						return 'Disetujui (Catatan)';
					}else if(rowData.flag == 'R'){
						return 'Revisi';
					}else if(rowData.flag == 'D'){
						return 'Faktur Dihapus';
					}else{
						return 'Faktur Selesai';
					}
				}
			}
			
        ];
		pagesizeboy = '50';
        tBar[modul_anak] = '#'+modul_anak+'_'+modul_main+'_toolbar';
		
		
		
	break;
	case "pemesanan_cabang":
	
		
		$('#tgl_awal_'+modul_anak).datebox({formatter: formatDate });
		$('#tgl_akhir_'+modul_anak).datebox({formatter: formatDate });
		$('#tgl_awal_'+modul_anak).datebox('setValue',today);
		$('#tgl_akhir_'+modul_anak).datebox('setValue',today);
		
       // url1 = 
		w_window=frmWidth-280;
		h_window=frmHeight-252;
		kolom[modul_anak] = [
			{field:'ck', checkbox:true},
           
			{field:'no_pemesanan',title:'No. Pemesanan',hidden:false,width:150},
			{field:'tgl_pemesanan',title:'Tgl. Pemesanan',width:150,align:'center'},
			{field:'nama_dealer',title:'Dealer',width:200,align:'left'},
			{field:'flag',title:'Status',width:150,align:'center',
				styler:function(index,rowData){  
					if(rowData.flag == 'P'){
						return 'background-color:#99B700;color:#F3F3F3';
					}else if(rowData.flag == 'B'){
						return 'background-color:red;color:#F3F3F3';
					}else if(rowData.flag == 'C'){
						return 'background-color:#F7E15A;color:navy;';
					}else if(rowData.flag == 'R'){
						return 'background-color:#A67334;color:#F3F3F3';
					}else if(rowData.flag == 'D'){
						return 'background-color:red;color:#F3F3F3';
					}else{
						return '';
					}
							
				},
				formatter:function(value,rowData,rowIndex){
					if(rowData.flag == 'P'){
						return 'Proses';
					}else if(rowData.flag == 'B'){
						return 'Batal';
					}else if(rowData.flag == 'C'){
						return 'Disetujui (Catatan)';
					}else if(rowData.flag == 'R'){
						return 'Revisi';
					}else if(rowData.flag == 'D'){
						return 'Pengajuan Dihapus';
					}else{
						return 'Disetujui';
					}
				}
			},	
		{field:'create_by',title:'Diajukan Oleh',width:200,align:'left'},			
			
			
			
        ];
		pagesizeboy = '50';
        tBar[modul_anak] = '#'+modul_anak+'_'+modul_main+'_toolbar';
		
		
		
	break;
	case "pembelian":
		$('#tgl_faktur_awal').datebox({formatter: formatDate });
		$('#tgl_faktur_akhir').datebox({formatter: formatDate });
		$('#tgl_faktur_awal').datebox('setValue',today);
		$('#tgl_faktur_akhir').datebox('setValue',today);
		//url1 = host+"procurement/get_data/"+modul_anak;
		w_window=frmWidth-280;
		h_window=frmHeight-310;
		kolom[modul_anak] = [
			{field:'ck', checkbox:true},
            {field:'no_faktur_pembelian',title:'No. Faktur',hidden:false,width:150},
			{field:'tgl_faktur',title:'Tgl. Faktur',width:100,align:'center'},
			{field:'jenis_pembelian',title:'Jenis Pembelian',hidden:false,width:150},
			{field:'jenis_pembayaran',title:'Jenis Pembayaran',width:150,align:'left'},
			{field:'cara_pembayaran',title:'Cara Pembayaran',width:150,align:'left'},
			{field:'tot_nilai_faktur',title:'Tot. Nilai',width:100,align:'right'}							
			
        ];
		//tBar[modul_anak] = '#'+modul_anak+'_'+modul_main+'_toolbar';
	break;
	case "penerimaan":
		$('#tgl_penerimaan_awal').datebox({formatter: formatDate });
		$('#tgl_penerimaan_akhir').datebox({formatter: formatDate });
		$('#tgl_penerimaan_awal').datebox('setValue',today);
		$('#tgl_penerimaan_akhir').datebox('setValue',today);
		//url1 = host+"procurement/get_data/"+modul_anak;
		w_window=frmWidth-280;
		h_window=frmHeight-310;
		kolomkaku[modul_anak]=[
			{field:'ck', checkbox:true},
			{field:'id',title:'.::.',hidden:false,width:50,align:'center',
				formatter:function(value,rowData,rowIndex){
					if (rowData.flag=='R'){  
						return '<a href="javascript:void(0)" onClick="get_subdetil(\''+rowData.id+'\')" style="" title="Tambah / View Item Barang Rinci"><img src="'+host+'assets/css/themes_easyui/icons/eye.png" ></a>';
					}
					else{return '<img src="'+host+'assets/css/themes_easyui/icons/delete.png" title="Barang Belum Datang Semua">';}
				}
			
			},
            {field:'no_penerimaan',title:'No. Penerimaan',hidden:false,width:150},
			{field:'tgl_penerimaan',title:'Tgl. Penerimaan',width:100,align:'center'},		   
		];
		kolom[modul_anak] = [
			
			{field:'no_surat_jalan',title:'No. Surat Jalan',hidden:false,width:150},
			{field:'tgl_surat_jalan',title:'Tgl. Surat Jalan',width:150,align:'center'},
			{field:'diterima_oleh',title:'Diterima Oleh',width:150,align:'left'},
			{field:'nama_gudang',title:'Gudang',width:150,align:'left'},
			{field:'keterangan',title:'Keterangan',width:250,align:'left'}							
			
        ];
		
		
	break;
	
	case "retur" :
		$('#tgl_retur_awal').datebox({formatter: formatDate });
		$('#tgl_retur_akhir').datebox({formatter: formatDate });
		$('#tgl_retur_awal').datebox('setValue',today);
		$('#tgl_retur_akhir').datebox('setValue',today);
		//url1 = host+"procurement/get_data/"+modul_anak;
		w_window=frmWidth-280;
		h_window=frmHeight-320;
		kolomkaku[modul_anak]=[
			{field:'ck', checkbox:true},
            {field:'no_retur',title:'No. Retur',hidden:false,width:150},
			{field:'tgl_retur',title:'Tgl. Retur',width:100,align:'center'},		   
		];
		kolom[modul_anak] = [
			{field:'no_faktur_pembelian',title:'No Faktur',width:250,align:'left'},	
			{field:'tgl_faktur',title:'Tgl. Faktur',width:100,align:'center'},		   
			{field:'kondisi_retur',title:'Kondisi',hidden:false,width:350},
			
        ];
		
		
	break;
	
}
tabel=$("#"+modul_anak+'_'+modul_main+"_grid").datagrid({        
		title:modul_anak.toUpperCase()+' DIVISI '+judul.toUpperCase(),
        height:h_window,
        width:w_window,
		iconCls: 'table',
		rownumbers:true,
        fit:false,
        striped:true,
        pagination:true,
        sortable:true,
        url:host+"procurement/get_data/"+modul_anak,		
		nowrap: false,
        singleSelect:true,
		pageNumber:'1',
		pageSize:pagesizeboy,
		queryParams:{
			modul_main:modul_main,
			flag_div:flag_div
		},
		frozenColumns:[
			 kolomkaku[modul_anak]         
		],	
        columns:[
            kolom[modul_anak]
        ],
		onLoadSuccess:function(d){
			//gridVRList.datagrid('selectRow', 0);
			$('.yes').linkbutton({  
					iconCls: 'cancel'  
			});
			$('.no').linkbutton({  
					iconCls: 'accept'  
			});
			
			$('.detil').linkbutton({  
					iconCls: 'icon-add'  
			});
			
		},
        toolbar: tBar[modul_anak],
		rowStyler:function(index,row){  
		 	if(modul_anak=='pemesanan'){
				if (row.flag=='' || row.flag==null){  
					return 'background-color:#A5C88D; color:navy;';  
				} 
				if (row.flag=='D'){  
					return 'background-color:red; color:yellow;';  
				} 
				//else{retur}
				
				
			}
			if(modul_anak=='penerimaan'){
				if (row.flag=='C'){  
					return 'background-color:yellow; color:navy;';  
				} 
			}
		},
		/*onDblClickRow:function(rowIndex, rowData){
			if(type=='vr'){
				$.get(host+'ord_mng/get_formna/list_detail/'+rowData.vr_submit_no+'/'+rowData.commodity_code,function(r){
					windowForm2(r,'List Detail Verification Request',frmWidth-400,frmHeight-300);
				});
				
			}
		},
		
		  */
});

	function printdatavo(){
		//var gdvo = $("#"+type+"_Grid").datagrid('getSelected');
		//window.open(host+'ord_mng/printdatavo/'+gdvo.importer_id+'/'+gdvo.exporter_id+'/'+gdvo.io_number+'/'+gdvo.commodity_code, '_blank');
		
		var gdvo = $("#"+type+"_Grid").datagrid('getSelected');
		if(gdvo){
			loadingna();
			$.post('ord_mng/printdatavo',{io_number:gdvo.io_number, importer_id:gdvo.importer_id, exporter_id:gdvo.exporter_id, commodity_code:gdvo.commodity_code},function(resp){
				winLoadingClose();
				windowForm2(resp,'Generate Document Verification Order',frmWidth-100,frmHeight-25); 
			});
		}else{
			$.messager.alert('warning','Please Select Column','warning');
		}
	}
	
	function transfervo(){
		var gdvo = $("#"+type+"_Grid").datagrid('getSelected');
		if(gdvo){
			loadingna();
			$.post('ord_mng/transfervo',{'io_number': gdvo.io_number, 'port_code': gdvo.code_loading_port, 'commodity_code':gdvo.commodity_code},function(resp){
				if(resp == 1){
					$.messager.alert('warning','VO Was Sent','warning');
					$("#"+type+"_Grid").datagrid('reload',{field:$("#cat_vo").val(), val:val_nya,create_from:create_from,create_to:create_to});	
				}else if(resp == 0){
					$.messager.alert('warning','Data Subcont/KSO Branch Not Found!','warning');
				}
			});
		}else{
			$.messager.alert('warning','Please Select Column','warning');
		}
		
		winLoadingClose();
	}
	
	function cari_data(mod){
		var post={};
		switch (mod){
			case "pemesanan_cabang":
			case "pemesanan":
				post['tgl_awal']=$('#tgl_awal_'+modul_anak).datebox('getValue');
				post['tgl_akhir']=$('#tgl_akhir_'+modul_anak).datebox('getValue');
				post['flag_div']=flag_div;
				post['cat']=$('#cat_'+modul_anak).val();
				post['key']=$('#key_'+modul_anak).val();
				post['status']=$('#status').val();
			break;
		
		}
		tabel.datagrid('reload',post);
	}
	
	function aktif_non(vr,sts){
		loadingna();
		$.post(host+'ord_mng/update_status',{vr:vr,status:sts},function(r){
			if(r==1){
					var msg;
					if(sts=='Y')msg='VR Submit No '+vr+', Button Upload IS AKTIF \n and Notification Was Sent To Importer';
					else msg='VR Submit No '+vr+', Button Upload IS NON AKTIF';
					$.messager.alert('Set Aktif/NonAktif',msg,'info');
					$("#"+type+"_Grid").datagrid('reload');
					winLoadingClose();
			}
			else{
				alert(r);winLoadingClose();	
			}
		});
		
	}
	
	$('#exp').bind('click',function(){
		var data={};
		data['field']=$("#cat_vr").val();
		data['val']=val_na;
		data['reg_from']=reg_from;
		data['reg_to']=reg_to;
		
		openWindowWithPost(host+'ord_mng/exp/vr_list',data);
			
	});
	
	function cek_grid(tbl){
		var row;
		row = $(tabel).datagrid('getSelected');
		var param={};
		if(tbl!='add'){	
			if(row){
				if(tbl=='delete'){
					if(row.flag=='P' || row.flag=='D'){
						$.messager.confirm('Confirm','Anda yakin akan menghapus data ini?',function(r){  
							if (r==true){  
								loadingna();
								$.post(host+'procurement/hapus/'+modul_anak,row,function(resp){
									if(resp==1){
										$.messager.alert('Delete Data','Data Sudah Terhapus','info');
										$(tabel).datagrid('reload');
										winLoadingClose();
									}
									else{
										$.messager.alert('Delete Data',resp,'error');
										winLoadingClose();
									}								
								});	
							}  
						}); 
					}
					else{
						$.messager.alert('Delete Data','Anda tidak berhak menghapus data ini karena terkait proses yang lain','error');
					}
				}
				else{
					console.log(row.flag);
					if (row.flag!='' && row.flag!=null ){ 
						var post={};
						post['sts']='edit';
						post['id']=row.id;
						post['flag_div']=flag_div;
						if(modul_anak=='pemesanan'){
							post['no_pemesanan_cabang']=row.no_permintaan;
							post['id_pemesanan_cabang']=row.id_cabang;
							post['tgl_pemesanan_cabang']=row.tgl_permintaan;
						}
						//$('#').toggle('slow');
						$('#isi_na_'+modul_anak).css('display','none');
						$('#form_naa_'+modul_anak).css('display','inline-block');
						$('#form_naa_'+modul_anak).html('').addClass('loading');
						$.post(host+'procurement/get_form/'+modul_anak,post,function(r){
							//windowForm(r,title,w,h);
							$('#form_naa_'+modul_anak).addClass('loading').html(r);
						});
					}
					else{
						$.messager.alert('Edit Data','Silahkan anda Tambah data terlebih dahulu','error');
					}
				}
			}
			else{$.messager.alert("Warning","Please Selected Data",'warning');}
		}
		else{
			if(modul_anak=='pemesanan'){
				if(row){
					var post={};
					post['sts']='add';
					post['flag_div']=flag_div;
					post['no_pemesanan_cabang']=row.no_permintaan;
					post['id_pemesanan_cabang']=row.id_cabang;
					post['tgl_pemesanan_cabang']=row.tgl_permintaan;
					//$('#').toggle('slow');
					$('#isi_na_'+modul_anak).css('display','none');
					$('#form_naa_'+modul_anak).css('display','inline-block');
					$('#form_naa_'+modul_anak).html('').addClass('loading');
					$.post(host+'procurement/get_form/'+modul_anak,post,function(r){
						//windowForm(r,title,w,h);
						$('#form_naa_'+modul_anak).addClass('loading').html(r);
					});
				}	
				else{
					$.messager.alert("Warning","Please Selected Data",'warning');
				}
			}
			else{
		
				var post={};
				post['sts']='add';
				post['flag_div']=flag_div;
				//$('#').toggle('slow');
				$('#isi_na_'+modul_anak).css('display','none');
				$('#form_naa_'+modul_anak).css('display','inline-block');
				$('#form_naa_'+modul_anak).html('').addClass('loading');
				$.post(host+'procurement/get_form/'+modul_anak,post,function(r){
					//windowForm(r,title,w,h);
					$('#form_naa_'+modul_anak).addClass('loading').html(r);
				});
				//get_form(host+'procurement/get_form/'+modul_anak,post,w_window,h_window,'Tambah Baru Pemesanan');
			}
		}
}

function get_subdetil(id){
	$.post(host+'procurement/get_form/subdetil_penerimaan',{id_penerimaan:id},function(r){
		windowForm(r,'View / Tambah Data PerItem Barang',(getClientWidth()-300),(getClientHeight()-250));
		
	});	
}
function get_subform(id,qty,kategori,satuan,nama){
	$('#grid_detil').css('display','none');
	$('#form_detil').css('display','inline');
	$('#isi_detil').html('').addClass('loading');
	$.post(host+'procurement/get_table/subdetil_penerimaan',{id:id,qty:qty,kategori:kategori,satuan:satuan,nama:nama},function(r){
		$('#isi_detil').removeClass('loading').html(r);
	});	
}
